package com.swotplus.admin.web.monitor;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.swotplus.framework.web.BaseController;
import com.swotplus.common.annotation.Log;
import com.swotplus.common.core.AjaxResult;
import com.swotplus.common.enums.BusinessType;
import com.swotplus.common.utils.StringUtils;
import com.swotplus.system.sys.entity.SysOperLog;
import com.swotplus.system.sys.service.SysOperLogService;
import com.swotplus.system.sys.vo.SysOperLogVo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 操作日志记录
 * 
 * @author hugang
 */
@Controller
@RequestMapping("/monitor/operlog")
public class SysOperlogController extends BaseController
{
    private String prefix = "monitor/operlog";

    @Autowired
    private SysOperLogService sysOperLogService;

    @RequiresPermissions("monitor:operlog:view")
    @GetMapping()
    public String operlog()
    {
        return prefix + "/list";
    }

    @RequiresPermissions("monitor:operlog:list")
    @PostMapping("/list")
    @ResponseBody
    public AjaxResult list(SysOperLogVo vo)
    {
        Page<SysOperLog> page=new Page<>(vo.getPage(),vo.getLimit());
        QueryWrapper<SysOperLog> queryWrapper=new QueryWrapper<>();
        if(StringUtils.isNotBlank(vo.getOperName())){
            queryWrapper.eq("oper_name",vo.getOperName());
        }
        if(StringUtils.isNotBlank(vo.getTitle())){
            queryWrapper.eq("title",vo.getTitle());
        }
        if(StringUtils.isNotBlank(vo.getMethod())){
            queryWrapper.eq("method",vo.getMethod());
        }
        IPage<SysOperLog> pageList= sysOperLogService.page(page,queryWrapper);
        return AjaxResult.success("成功", (int) pageList.getTotal(),pageList.getRecords());
    }


    @RequiresPermissions("monitor:operlog:remove")
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(sysOperLogService.removeById(ids));
    }

    @RequiresPermissions("monitor:operlog:detail")
    @GetMapping("/detail/{operId}")
    public String detail(@PathVariable("operId") String operId, ModelMap mmap)
    {
        mmap.put("operLog", sysOperLogService.getById(operId));
        return prefix + "/detail";
    }
    
    @Log(title = "操作日志", businessType = BusinessType.CLEAN)
    @RequiresPermissions("monitor:operlog:remove")
    @PostMapping("/clean")
    @ResponseBody
    public AjaxResult clean()
    {
        sysOperLogService.cleanOperLog();
        return success();
    }
}
